package action.user;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import org.hibernate.*;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

import db.*;

@SuppressWarnings("serial")
public class ServiceContractAction extends ActionSupport implements Preparable{
	private ServiceContract sc;
	private String sc_type;
	private List<String> sc_types;
	private String newSC_reId;
	private String new_scId;
	
	public void prepare() {
		sc_types = new ArrayList<String>();
		sc_types.add("Seller's");
		sc_types.add("Buyer's");
    }
	
	public ServiceContract getSc() {
		return sc;
	}

	public void setSc(ServiceContract sc) {
		this.sc = sc;
	}

	public String getSc_type() {
		return sc_type;
	}

	public void setSc_type(String scType) {
		sc_type = scType;
	}

	public List<String> getSc_types() {
		return sc_types;
	}

	public void setSc_types(List<String> scTypes) {
		sc_types = new ArrayList<String>();
		sc_types.add("Seller's");
		sc_types.add("Buyer's");
		//sc_types= scTypes;
	}

	public String getNewSC_reId() {
		return newSC_reId;
	}

	public void setNewSC_reId(String newSCReId) {
		newSC_reId = newSCReId;
	}

	public String getNew_scId() {
		return new_scId;
	}

	public void setNew_scId(String newScId) {
		new_scId = newScId;
	}

	@SuppressWarnings("unchecked")
	public String add(){
		Map session = ActionContext.getContext().getSession();
		//HttpServletRequest request = ServletActionContext.getRequest();
		int reId = Integer.parseInt(this.getNewSC_reId());
		String userId = (String)session.get("userId");
		int uId = Integer.parseInt(userId);
		ServiceContractDAO scDao = new ServiceContractDAO();
		org.hibernate.Transaction trans = scDao.getSession().beginTransaction();
		ServiceContract scToAdd = new ServiceContract();
		scToAdd.setReId(reId);
		scToAdd.setScStatus(0);// 0 for initial, 1 for in-progress, 2 for done (after sale)
		Date now = new Date();
		now.getTime();
		scToAdd.setScDate(now);
		int sc_type_int = (getSc_type().equals("Seller's"))?1:0; 
		scToAdd.setScType(sc_type_int);// 1 for selling, 0 for buying
		scToAdd.setUId(uId);
		scToAdd.setScDescription(sc.getScDescription());
		scDao.save(scToAdd);
		trans.commit();
		//System.out.println("new id is: "+);
		this.setNew_scId(scToAdd.getScId()+"");
		return SUCCESS;
	}
}
